#!/usr/bin/python3
# coding: utf-8
# authon: ren
import time
# from rest_framework.throttling import SimpleRateThrottle

VISIT_RECORD = {}
class VisitThrottle(object):

    def __init__(self):
        self.history = None

    def allow_request(self,request,view):
        # 1.获取用户IP
        remote_addr = request.META.get('REMOTE_ADDR')
        ctime = time.time()
        if remote_addr not in VISIT_RECORD:
            VISIT_RECORD[remote_addr] = [ctime,]
            return True
        history = VISIT_RECORD.get(remote_addr)
        self.history = history

        while history and history[-1] < ctime - 60:
            history.pop()

        if len(history)<3:
            history.insert(0,ctime)
            #  True 表示可以继续访问，False 表示：访问频率太高，被限制
            return  True

# class VisitThrottlel(SimpleRateThrottle):
#     scope = 'Luffy'
#
#     def get_cache_key(self, request, view):
#         return self.get_ident(request)